package cn.wildfire.chat.kit.conversation.message.viewholder;

import android.view.View;
import android.widget.TextView;

import com.sourcemuch.im.caiba.R;
import com.sourcemuch.im.caiba.R2;

import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import cn.wildfire.chat.kit.annotation.LayoutRes;
import cn.wildfire.chat.kit.annotation.MessageContentType;
import cn.wildfire.chat.kit.conversation.message.model.UiMessage;
import cn.wildfirechat.message.notification.AddGroupMemberNotificationContent;
import cn.wildfirechat.message.notification.ChangeGroupNameNotificationContent;
import cn.wildfirechat.message.notification.ChangeGroupPortraitNotificationContent;
import cn.wildfirechat.message.notification.CreateGroupNotificationContent;
import cn.wildfirechat.message.notification.DismissGroupNotificationContent;
import cn.wildfirechat.message.notification.GroupJoinTypeNotificationContent;
import cn.wildfirechat.message.notification.GroupMuteNotificationContent;
import cn.wildfirechat.message.notification.GroupPrivateChatNotificationContent;
import cn.wildfirechat.message.notification.GroupSetManagerChatNotificationContent;
import cn.wildfirechat.message.notification.KickoffGroupMemberNotificationContent;
import cn.wildfirechat.message.notification.ModifyGroupAliasNotificationContent;
import cn.wildfirechat.message.notification.NotificationMessageContent;
import cn.wildfirechat.message.notification.QuitGroupNotificationContent;
import cn.wildfirechat.message.notification.RecallMessageContent;
import cn.wildfirechat.message.notification.SingleSilenceGroupNotificationContent;
import cn.wildfirechat.message.notification.TipNotificationContent;
import cn.wildfirechat.message.notification.TransferGroupOwnerNotificationContent;
import cn.wildfirechat.model.UserInfo;
import cn.wildfirechat.remote.ChatManager;

/**
 * 小灰条消息, 居中显示，且不显示发送者，用于简单通知，如果需要扩展成复杂通知，可以参考 {@link ExampleRichNotificationMessageContentViewHolder}
 *
 */
@MessageContentType(value = {
        AddGroupMemberNotificationContent.class,
        ChangeGroupNameNotificationContent.class,
        ChangeGroupPortraitNotificationContent.class,
        CreateGroupNotificationContent.class,
        DismissGroupNotificationContent.class,
        DismissGroupNotificationContent.class,
        KickoffGroupMemberNotificationContent.class,
        ModifyGroupAliasNotificationContent.class,
        QuitGroupNotificationContent.class,
        TransferGroupOwnerNotificationContent.class,
        TipNotificationContent.class,
        RecallMessageContent.class,
        GroupMuteNotificationContent.class,
        GroupPrivateChatNotificationContent.class,
        GroupJoinTypeNotificationContent.class,
        GroupSetManagerChatNotificationContent.class,
        // TODO add more
        SingleSilenceGroupNotificationContent.class
})
@LayoutRes(resId = R2.layout.conversation_item_notification)
public class SimpleNotificationMessageContentViewHolder extends MessageContentViewHolder {

    @BindView(R2.id.notificationTextView)
    TextView notificationTextView;

    /**
    * modify by HEKE , 2019/10/16 14:43 刷新显示需求
    */
    protected OnSimpleNotificationBindEndListener onSimpleNotificationBindEndListener;
    protected int currentNeedCheckMessageHashcode;

    public SimpleNotificationMessageContentViewHolder(FragmentActivity activity, RecyclerView.Adapter adapter, View itemView) {
        super(activity, adapter, itemView);
    }

    public void setOnSimpleNotificationBindEndListener(OnSimpleNotificationBindEndListener onSimpleNotificationBindEndListener) {
        this.onSimpleNotificationBindEndListener = onSimpleNotificationBindEndListener;
    }

    public void setCurrentNeedCheckMessageHashcode(int currentNeedCheckMessageHashcode) {
        this.currentNeedCheckMessageHashcode = currentNeedCheckMessageHashcode;
    }

    @Override
    public void onBind(UiMessage message, int position) {
        super.onBind(message, position);
        onBind(message);
    }

    @Override
    public boolean contextMenuItemFilter(UiMessage uiMessage, String tag) {
        return true;
    }

    protected void onBind(UiMessage message) {
        try {
        String notification;
        try {
            notification = ((NotificationMessageContent) message.message.content).formatNotification(message.message);
        } catch (Exception e) {
            e.printStackTrace();
            notification = "message is invalid";
        }
        //修改在成员扫描群二维码加入群聊时的文字提示
        if (notification.contains(context.getString(R.string.text_invitation)) && notification.contains(context.getString(R.string.text_join_group))) {
            String sender = notification.substring(0, notification.indexOf(context.getString(R.string.text_invitation)));
            String member = notification.substring(notification.indexOf(context.getString(R.string.text_invitation)) + 2, notification.indexOf(context.getString(R.string.text_join_group)));
            if (sender.trim().equals(member.trim())) {
                notification = member + context.getString(R.string.join_groups_by_scanning_qr_code);
            }
        }

        //如果是自身扫描二维码进入群组,修改自身看到的群组消息
        if (notification.contains(context.getString(R.string.yourself_invited)) && notification.contains(context.getString(R.string.text_join_group))) {
            UserInfo userInfo = userViewModel.getUserInfo(ChatManager.Instance().getUserId(), false);
            String inviteMyself = notification.substring(notification.indexOf(context.getString(R.string.yourself_invited)) + 3, notification.indexOf(context.getString(R.string.text_join_group)));
            if (userInfo.displayName.equals(inviteMyself.trim())) {
                notification = context.getString(R.string.yourself_join_group_by_scanning_qr_code);
            }
        }

        notificationTextView.setText(notification);

        // 执行回调
        if(onSimpleNotificationBindEndListener != null){
            if(currentNeedCheckMessageHashcode == message.hashCode()) {
                currentNeedCheckMessageHashcode = -1;
                NotificationMessageContent notificationMessageContent = (NotificationMessageContent) message.message.content;
                onSimpleNotificationBindEndListener.notifyEnd(notificationMessageContent);
            }
        }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
    *  Create by HEKE, 2019/10/16 14:20
    *  TODO 聊天页面基本通知接收结束后进行回调监听接口
    */
    public interface OnSimpleNotificationBindEndListener{
        void notifyEnd(NotificationMessageContent notificationMessageContent);
    }

}
